﻿/******************************************************    
文件名:GetSteelData.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-12-07
维护人员:黄媛媛
维护日期:2010-12-07
文件功能描述:金属材料热处理网络学习平台_访问牌号
******************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using Mmht.DB;
using System.Configuration;
using System.Collections;

namespace Mmht.BT
{
    public class GetSteelData
    {
        TableSteelDataDB ts = new TableSteelDataDB();

        public DataTable search_Steel(string table_name,string key)
        {
            switch (table_name)
            {
                case "steel_composition":
                    return search_Table(table_name, key);
                case "materials_number":
                    return search_Table(table_name, key);
                case "structure_property":
                    return ts.search_StructureProperty(key);
                case "heat_treating_data":
                    return ts.search_HeatTreatingData(key);
                default:
                    return null;
            }
        }

        /// <summary>
        /// 牌号及其化学成分查询
        /// </summary>
        /// <returns></returns>
        public DataTable get_SteelComposition()
        {
            return get_Table(1);
        }

        /// <summary>
        /// 材料编号查询
        /// </summary>
        /// <returns></returns>
        public DataTable get_MaterialsNumber()
        {
            return get_Table(2);
        }

        /// <summary>
        /// 牌号及其化学成分查询
        /// 或材料编号查询
        /// </summary>
        /// <param name="table_type">数据表类型</param>
        /// <param name="table_type">1SteelComposition</param>
        /// <param name="table_type">2MaterialsNumber</param>
        /// <returns></returns>
        private DataTable get_Table(int table_type)
        {
            DataTable table=new DataTable();//信息整合后数据表
            table = get_Table_col(table_type);

            DataTable dt = new DataTable();
            dt = ts.get_Tableleft(table_type);
            for (int i = 0; i < dt.Rows.Count; i++)
            { 
                DataRow row = table.NewRow();
                //生成新条目，合并两张表的信息

                for(int j=0;j<dt.Columns.Count;j++)
                {
                    row[dt.Columns[j].ColumnName] = dt.Rows[i][j].ToString();
                }
                int s_id = Convert.ToInt32(dt.Rows[i]["id"]);
                DataTable dtx = new DataTable();
                dtx = ts.get_XContent(table_type, s_id);
                for (int j = 0; j < dtx.Rows.Count; j++)
                {
                    row[dtx.Rows[j]["x"].ToString()] = dtx.Rows[j]["x_content"].ToString();
                }
                table.Rows.Add(row);
            }
            return table;
        }

        private DataTable get_Table_col(int table_type)
        {
            DataTable table = new DataTable();
            DataTable dt = new DataTable();
            dt = ts.get_Tableleft(table_type);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                table.Columns.Add(dt.Columns[i].ColumnName);
            }
            dt = ts.get_X(table_type);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                table.Columns.Add(dt.Rows[i]["x"].ToString());
            }
            return table;
        }

        /// <summary>
        /// 组织和性能数据查询
        /// </summary>
        /// <returns></returns>
        public DataTable get_StructureProperty()
        {
            return ts.get_StructureProperty();
        }

        /// <summary>
        /// 热处理数据查询
        /// </summary>
        /// <returns></returns>
        public DataTable get_HeatTreatingData()
        {
            return ts.get_HeatTreatingData();
        }


        /// <summary>
        /// 牌号及其化学成分查询
        /// 或材料编号查询
        /// </summary>
        /// <param name="table_type">数据表类型</param>
        /// <param name="table_type">1SteelComposition</param>
        /// <param name="table_type">2MaterialsNumber</param>
        /// <returns></returns>
        private DataTable search_Table(string table_name,string key)
        {
            DataTable table = new DataTable();//信息整合后数据表
            int table_type = 0;
            switch (table_name)
            {
                case "steel_composition": table_type = 1; break;
                case "materials_number": table_type = 2; break;
                default: table_type = 0; break;
            }
            table = ts.search_Tableleft(table_name, key);
            table = search_Table_col(table, table_type);

            DataTable dt = new DataTable();
            dt = ts.search_Tableleft(table_name, key);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = table.NewRow();
                //生成新条目，合并两张表的信息

                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    row[dt.Columns[j].ColumnName] = dt.Rows[i][j].ToString();
                }
                int s_id = Convert.ToInt32(dt.Rows[i]["id"]);
                DataTable dtx = new DataTable();
                dtx = ts.get_XContent(table_type, s_id);
                for (int j = 0; j < dtx.Rows.Count; j++)
                {
                    row[dtx.Rows[j]["x"].ToString()] = dtx.Rows[j]["x_content"].ToString();
                }
                table.Rows.Add(row);
            }
            return table;
        }

        private DataTable search_Table_col(DataTable dt0,int table_type)
        {
            DataTable table = new DataTable();
            DataTable dt = new DataTable();
            dt = dt0;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                table.Columns.Add(dt.Columns[i].ColumnName);
            }
            dt = ts.get_X(table_type);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                table.Columns.Add(dt.Rows[i]["x"].ToString());
            }
            return table;
        }
    }


}
